// ==========================================================================
// gin-sns自动生成业务逻辑层相关代码，只生成一次，按需修改,再次生成不会覆盖.
// date：2021-02-21 13:45:54 +0800 CST
// path: app/service/module/rss_service.go
// author：jon
// ==========================================================================
package module

import (
	"errors"
	"gin-sns/app/dao/module/rss"
	rssModel "gin-sns/app/model/module/rss"
	"time"

	"github.com/gin-gonic/gin"

	"gin-sns/app/ginframe/utils/convert"
	"gin-sns/app/ginframe/utils/page"
)

var RssService = newRssService()

func newRssService() *rssService {
	return &rssService{}
}

type rssService struct {
}

//根据主键查询数据
func (s *rssService) SelectRecordById(id int64) (*rssModel.Entity, error) {
	return rss.RssDao.SelectById(id)
}

//
func (s *rssService) SelectRecordsIn(idarr []int64) ([]rssModel.Entity, error) {

	list, err := rss.RssDao.FindIn("id", idarr)
	if list != nil && len(list) > 0 {
		err = errors.New("not found")
		list = nil
	}
	return list, err
}

//根据主键删除数据
func (s *rssService) DeleteRecordById(id int64) bool {
	return rss.RssDao.DeleteById(id)
}

//批量删除数据记录
func (s *rssService) DeleteRecordByIds(ids string) int64 {
	ida := convert.ToInt64Array(ids, ",")
	result, err := rss.RssDao.DeleteBatch(ida...)
	if err != nil {
		return 0
	}
	return result
}

//添加数据
func (s *rssService) AddSave(req *rssModel.AddReq, ctx *gin.Context) (int64, error) {
	var entity rssModel.Entity

	//entity.Id = req.Id
	entity.Category = req.Category
	entity.UserId = req.UserId
	entity.Title = req.Title
	entity.Description = req.Description
	entity.IconUrl = req.IconUrl
	entity.Url = req.Url
	entity.CountPost = req.CountPost
	entity.CountFollow = req.CountFollow
	entity.Status = req.Status
	entity.CreateTime = time.Now()
	//entity.CreateBy = ""

	//user := system.UserService.GetProfile(ctx)

	//if user != nil {
	//	entity.CreateBy = user.LoginName
	//}

	_, err := rss.RssDao.Insert(&entity)
	return entity.Id, err
}

//修改数据
func (s *rssService) EditSave(req *rssModel.EditReq, ctx *gin.Context) (int64, error) {
	entity := &rssModel.Entity{Id: req.Id}
	ok, err := rss.RssDao.FindOne(entity)

	if err != nil {
		return 0, err
	}

	if !ok {
		return 0, errors.New("数据不存在")
	}

	entity.Category = req.Category
	entity.UserId = req.UserId
	entity.Title = req.Title
	entity.Description = req.Description
	entity.IconUrl = req.IconUrl
	entity.Url = req.Url
	entity.CountPost = req.CountPost
	entity.CountFollow = req.CountFollow
	entity.Status = req.Status
	//entity.UpdateTime = time.Now()
	//entity.UpdateBy = ""

	//user := system.UserService.GetProfile(ctx)

	//if user == nil {
	//	entity.UpdateBy = user.LoginName
	//}

	return rss.RssDao.Update(entity)
}

//根据条件查询数据
func (s *rssService) SelectListAll(params *rssModel.SelectPageReq) ([]rssModel.Entity, error) {
	return rss.RssDao.SelectListAll(params)
}

//根据条件分页查询数据
func (s *rssService) SelectListByPage(params *rssModel.SelectPageReq) ([]rssModel.Entity, *page.Paging, error) {
	return rss.RssDao.SelectListByPage(params)
}

// 导出excel
func (s *rssService) Export(param *rssModel.SelectPageReq) (string, error) {
	head := []string{"", "分类", "创建用户", "标题", "描述", "封面URL", "RSS URL", "文章数量", "被关注数量", "0未审核 1正常状态 2审核未通过 3删除", "创建时间", "更新时间"}
	col := []string{"id", "category", "user_id", "title", "description", "icon_url", "url", "count_post", "count_follow", "status", "create_time", "update_time"}
	return rss.RssDao.SelectListExport(param, head, col)
}

// 浏览数+1
func (s *rssService) IncrFollowCount(rssId int64) {
	rss.RssDao.IncrFollowCount(rssId)
}

func (s *rssService) DecrFollowCount(rssId int64) {
	rss.RssDao.DecrFollowCount(rssId)
}
